import type {SysUserResp} from '#/type/sys_user';

import {baseRequestClient, requestClient} from '#/api/request';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    password?: string;
    username?: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    access_token: string;
    token_type: string;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }
}

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  return requestClient.post<AuthApi.LoginResult>('/auth/login', data, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  });
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
    withCredentials: true,
  });
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return baseRequestClient.post('/auth/logout', {
    withCredentials: true,
  });
}

/**
 * 获取登录用户信息
 */
export async function getUserInfoApi() {
  return requestClient.get<SysUserResp>('/auth/get_login_user_info');
}

/**
 * 获取登录用户权限码
 */
export async function getAccessCodesApi() {
  return requestClient.get<string[]>('/auth/get_auth_codes');
}

export async function getAccessRolesApi() {
  return requestClient.get<string[]>('/auth/get_auth_roles');
}
